package ui.fragment.notifications;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.ArrayList;

import adapter.notifications.CompanyNotificationsFragmentAdapter;
import controller.group_management.GroupManagementAsync;
import controller.notifcations.CompanyNotificationsAsync;
import listener.notifications.CompanyNotificationsScrollListener;
import ui.activity.login.Login;

public class CompanyNotificationsFragment extends Fragment
        implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener{

    /**
     * String section
     */

    /**
     * View section
     */
    public static ListView mLvInCompanyNotificationsFragment;
    public static CompanyNotificationsFragmentAdapter companyNotificationsFragmentAdapter;
    private SwipeRefreshLayout mSrlInCompanyNotificationsFragment;
    private static Context mContext;
    /**
     * Others section
     */

    /**
     * Listener section
     */

    public static Fragment newInstance() {

        CompanyNotificationsFragment fragment = new CompanyNotificationsFragment();
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = getLayoutInflater(savedInstanceState).inflate(
                R.layout.fragment_company_notifications, container, false);
        mContext = getActivity();
        // Initial views
        initialViews(v);
        initialData();
        setDataForCompanyNotificationsFragment();
        return v;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //clear data
        if (!CompanyNotificationsAsync.mAlCompanyNotifications.isEmpty())
            CompanyNotificationsAsync.mAlCompanyNotifications.clear();
        if (!CompanyNotificationsAsync.mAlId.isEmpty())
            CompanyNotificationsAsync.mAlId.clear();
        CompanyNotificationsScrollListener.visibleThreshold = 5;
        CompanyNotificationsScrollListener.currentPage = 0;
        CompanyNotificationsScrollListener.previousTotal = 0;
        CompanyNotificationsScrollListener.loading = true;
        //TODO will remove after finish all ibtn on actionbar
        //TODO mean write function invisible all the others ibtn and only visible ibtn at current page
        //invisible ibtn create group
        Enterprise.mIbtnCreateGroup.setVisibility(View.GONE);
        Enterprise.mIbtnUpload.setVisibility(View.VISIBLE);
        Enterprise.mIbtnInviteUserToCompany.setVisibility(View.GONE);

    }

    @Override
    public void onRefresh() {
        // what's thing need refresh when pull down, need place in here
        try {
            //Call new thread to get Data
            Log.i("", "refresh data");
            if (!CompanyNotificationsAsync.mAlId.isEmpty())
                CompanyNotificationsAsync.mAlId.clear();
            if (!CompanyNotificationsAsync.mAlCompanyNotifications.isEmpty())
                CompanyNotificationsAsync.mAlCompanyNotifications.clear();
            companyNotificationsFragmentAdapter.notifyDataSetChanged();
            //clear on scroll listener
            CompanyNotificationsScrollListener.visibleThreshold = 0;
            CompanyNotificationsScrollListener.currentPage = 0;
            CompanyNotificationsScrollListener.previousTotal = 0;
            CompanyNotificationsScrollListener.loading = true;

            setDataForCompanyNotificationsFragment();

        } catch (Exception e) {
            e.printStackTrace();
        }
        // what's thing need refresh when pull down, need place in here
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    // Stop the animation once we are done fetching data.
                    mSrlInCompanyNotificationsFragment.setRefreshing(false);
                    /**
                     * You can add code to update your list with the new data.
                     **/
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 4000);
    }

    /**
     * Initialize methods
     */
    private void initialData() {
        companyNotificationsFragmentAdapter = new CompanyNotificationsFragmentAdapter(getActivity(),
                R.layout.simple_list_item_in_company_notifications_fragment,
                CompanyNotificationsAsync.mAlCompanyNotifications);
        mLvInCompanyNotificationsFragment.setAdapter(companyNotificationsFragmentAdapter);
        mLvInCompanyNotificationsFragment.setOnScrollListener(
                new CompanyNotificationsScrollListener(getActivity(), 1));
        mSrlInCompanyNotificationsFragment.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light
        );
        mSrlInCompanyNotificationsFragment.setOnRefreshListener(this);
        //invisible al ibtn at action bar
        Enterprise.mIbtnCreateGroup.setVisibility(View.INVISIBLE);
        Enterprise.mIbtnUpload.setVisibility(View.GONE);
        Enterprise.mIbtnInviteUserToCompany.setVisibility(View.GONE);
    }

    private void initialViews(View v) {
        mLvInCompanyNotificationsFragment = (ListView) v.findViewById(
                R.id.lv_in_company_notifications_fragment);
        mSrlInCompanyNotificationsFragment = (SwipeRefreshLayout) v.findViewById(
                R.id.srl_in_company_notifications_fragment);
    }

    /**
     * The others methods
     */
    public static void setDataForCompanyNotificationsFragment() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new CompanyNotificationsAsync(mContext).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    Login.user.getUserInfo().getUserName(),
                    String.valueOf(Enterprise.company.getCompaniesOfUserInfoSelected().getUserCompanyId()),
                    mContext.getResources().getString(R.string.true_),
                    0 + "",
                    mContext.getResources().getString(R.string.enterprise_company)
            );
        else
            new CompanyNotificationsAsync(mContext).execute(
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    Login.user.getUserInfo().getUserName(),
                    String.valueOf(Enterprise.company.getCompaniesOfUserInfoSelected().getUserCompanyId()),
                    mContext.getResources().getString(R.string.true_),
                    0 + "",
                    mContext.getResources().getString(R.string.enterprise_company)
            );
    }
}
